package authoritah.ui.member;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.BadLocationException;

import authoritah.db.Member;
import authoritah.ui.member.popup.MemberPopupController;
import authoritah.ui.member.popup.MemberPopupModel;

public class MemberMainController {
	private MemberMainView view;

	private MemberMainModel model;

	public MemberMainController() {
		ArrayList<Member> members = Member.getAllMembers();
		model = new MemberMainModel(members);
		view = new MemberMainView(model);

		createActions();
		view.setIcons();
	}

	public JPanel getDisplayComponent() {
		return view;
	}

	private void createActions() {
		Action addAction = new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				MemberPopupController popupController = new MemberPopupController();
				popupController.getDialog().show();

				MemberPopupModel memberModel = popupController.getModel();

				try {
					String memberName = memberModel.getName().getText(0,
							memberModel.getName().getLength());

					if (memberModel.getName().getText(0,
							memberModel.getName().getLength()) != "") {

						Member member = new Member();

						member.setName(memberName);
						member.setInitialPoints(Integer.parseInt(memberModel
								.getInitialPoints().getText(
										0,
										memberModel.getInitialPoints()
												.getLength())));

						String status = (String) memberModel.getStatus()
								.getSelectedItem();
						if (status.equals("Normal")) {
							member.setStatus(0);
						} else if (status.equals("Dormant")) {
							member.setStatus(1);
						} else if (status.equals("Trial")) {
							member.setStatus(2);
						} else if (status.equals("Retrial")) {
							member.setStatus(3);
						}

						Member.createMember(member);
					}
				} catch (BadLocationException ex) {
					ex.printStackTrace(); // this will never occur.
				}

				model.updateData(Member.getAllMembers());
			}
		};
		view.setAddAction(addAction);

		Action editAction = new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				Member editMember = view.getSelectedMember();

				MemberPopupController popupController = new MemberPopupController(
						editMember);
				popupController.getDialog().show();

				MemberPopupModel memberModel = popupController.getModel();
				try {
					editMember.setName(memberModel.getName().getText(0,
							memberModel.getName().getLength()));
					editMember
							.setInitialPoints(Integer.parseInt(memberModel
									.getInitialPoints().getText(
											0,
											memberModel.getInitialPoints()
													.getLength())));
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
				editMember.setStatus(getStatus((String) memberModel.getStatus()
						.getSelectedItem()));
				Member.updateMember(editMember);

				// Finally refresh the screen.
				model.updateData(Member.getAllMembers());
			}
		};
		view.setEditAction(editAction);

		Action removeAction = new AbstractAction() {
			public void actionPerformed(ActionEvent arg0) {
				Member removeMember = view.getSelectedMember();
				// Confirm removal of selected member
				int yesNo = JOptionPane.showConfirmDialog(view,
						"Are you sure you wish to remove "
								+ removeMember.getName() + "?",
						"Remove Member", JOptionPane.YES_NO_OPTION);
				if (yesNo == JOptionPane.YES_OPTION) {
					Member.removeMember(removeMember);

					// Finally refresh the screen.
					model.updateData(Member.getAllMembers());
				}
			}
		};
		view.setRemoveAction(removeAction);
	}

	private int getStatus(String statusString) {
		if (statusString.equals("Normal")) {
			return 0;
		} else if (statusString.equals("Dormant")) {
			return 1;
		} else if (statusString.equals("Trial")) {
			return 2;
		} else if (statusString.equals("Retrial")) {
			return 3;
		} else {
			return 0;
		}
	}
}
